package com.hb.trade.server.project.evaluation.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hb.core.pojo.center.au.dto.UserDTO;
import com.hb.core.pojo.enums.EvaluationStatusEnum;
import com.hb.core.pojo.expert.dto.ExpertDTO;
import com.hb.core.pojo.parent.PageResult;
import com.hb.core.pojo.project.evaluation.dto.ConfirmationInfoDTO;
import com.hb.core.pojo.project.evaluation.dto.EvaluationCommitteeBidDTO;
import com.hb.core.pojo.project.evaluation.dto.EvaluationCommitteeDTO;
import com.hb.core.pojo.project.evaluation.dto.EvaluationCommitteeMembersDTO;
import com.hb.core.pojo.project.evaluation.entity.EvaluationCommitteeMembers;
import com.hb.core.pojo.project.file.dto.BidFileInfoDTO;
import com.hb.trade.server.project.evaluation.service.AntianaphylaxisService;
import com.hb.trade.server.project.evaluation.service.EvaluationCommitteeBidService;
import com.hb.trade.server.project.evaluation.service.EvaluationCommitteeMembersService;
import com.hb.trade.server.project.file.service.BidFileInfoService;
import org.fzzn.core.model.enums.LogicEnum;
import org.fzzn.core.model.enums.MsgEnum;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.util.Enums;
import org.fzzn.core.util.MsgUtil;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 评标委员会成员信息 前端控制器
 * </p>
 *
 * @author ljs
 * @since 2021-12-19 14:10:12
 */
@RestController
public class EvaluationCommitteeMembersController {


    protected final EvaluationCommitteeMembersService evaluationCommitteeMembersService;
    protected final EvaluationCommitteeBidService evaluationCommitteeBidService;
    protected final AntianaphylaxisService antianaphylaxisService;
    protected final BidFileInfoService bidFileInfoService;

    public EvaluationCommitteeMembersController(EvaluationCommitteeMembersService evaluationCommitteeMembersService,
                                                EvaluationCommitteeBidService evaluationCommitteeBidService,
                                                AntianaphylaxisService antianaphylaxisService,
                                                BidFileInfoService bidFileInfoService) {
        this.evaluationCommitteeMembersService = evaluationCommitteeMembersService;
        this.evaluationCommitteeBidService = evaluationCommitteeBidService;
        this.antianaphylaxisService = antianaphylaxisService;
        this.bidFileInfoService = bidFileInfoService;
    }


    /**
     * 根据评委会Id查询成员信息
     *
     * @param evaluationCommitteeDTO
     * @return
     */
    @PostMapping("/evaluationCommittee/listMembers")
    public Message<List<EvaluationCommitteeMembersDTO>> listMembers(@RequestBody EvaluationCommitteeDTO evaluationCommitteeDTO) {
        if (null == evaluationCommitteeDTO || null == evaluationCommitteeDTO.getId()) {
            return MsgUtil.fail("参数为空");
        }
        // 查询成员
        List<EvaluationCommitteeMembers> evaluationCommitteeMembersList = evaluationCommitteeMembersService.list(
                new LambdaQueryWrapper<EvaluationCommitteeMembers>().eq(EvaluationCommitteeMembers::getEvaluationCommitteeId, evaluationCommitteeDTO.getId()));
        // 对随机抽取的专家进行脱敏
        List<EvaluationCommitteeMembersDTO> antiMemberInfos = antianaphylaxisService.antiExpertList(evaluationCommitteeMembersList);
        return MsgUtil.success(antiMemberInfos);
    }

    /**
     * 根据评委会成员Id删除成员
     *
     * @param evaluationCommitteeMembersDTO
     * @return
     */
    @PostMapping("/evaluationCommittee/removeMember")
    public Message deleteMember(@RequestBody EvaluationCommitteeMembersDTO evaluationCommitteeMembersDTO) {
        if (null == evaluationCommitteeMembersDTO || null == evaluationCommitteeMembersDTO.getId()) {
            return MsgUtil.fail("参数为空");
        }
        boolean result = evaluationCommitteeMembersService.removeById(evaluationCommitteeMembersDTO.getId());
        return result ? MsgUtil.msg(MsgEnum.SUCCESS) : MsgUtil.fail(MsgEnum.FAILURE.msg());
    }

    /**
     * 随机抽取专家
     *
     * @param expertExtractList
     * @return
     */
    @PostMapping("/evaluationCommittee/extractingExpert")
    public Message extractingExpert (@RequestBody List<ExpertDTO> expertExtractList) {
        if (null == expertExtractList) {
            return MsgUtil.fail("参数为空");
        }
        List<EvaluationCommitteeMembersDTO> evaluationCommitteeMembers = new ArrayList<>();
        for(ExpertDTO expertDTO: expertExtractList){
            EvaluationCommitteeMembersDTO evaluationCommitteeMembersDTO = new EvaluationCommitteeMembersDTO();
            //start 赋值
            evaluationCommitteeMembersDTO.setMemberType(3);
            evaluationCommitteeMembersDTO.setId(expertDTO.getId());
            evaluationCommitteeMembersDTO.setMemberIdCard(expertDTO.getIdCard());
            evaluationCommitteeMembersDTO.setMemberName(expertDTO.getExpertName());
            evaluationCommitteeMembersDTO.setMemberCompany(expertDTO.getCompany());
            evaluationCommitteeMembersDTO.setMemberMobile(expertDTO.getPhoneNumber());
            evaluationCommitteeMembersDTO.setMajor(expertDTO.getMajorName());
            //ending 赋值
            EvaluationCommitteeMembersDTO antiEvaluationCommitteeMembersDTO = antianaphylaxisService.antiExpert(evaluationCommitteeMembersDTO);
            evaluationCommitteeMembers.add(antiEvaluationCommitteeMembersDTO);
        }
        return MsgUtil.success(evaluationCommitteeMembers);
    }

    /**
     * 根据用户名和密码，获取成员信息
     * @param userDTO
     * @return
     */
    @PostMapping("/evaluationCommittee/findMembersByIdCardAnPassword")
    public Message<EvaluationCommitteeMembersDTO> findMembersByIdCardAnPassword(@RequestBody UserDTO userDTO) {
        if (null == userDTO || null == userDTO.getUsername() || null == userDTO.getPassword()) {
            return MsgUtil.fail("参数为空");
        }

        EvaluationCommitteeMembersDTO evaluationCommitteeMember = evaluationCommitteeMembersService.findByMemberIdCardAndPassword(userDTO.getUsername(), userDTO.getPassword());

        if (evaluationCommitteeMember == null) {
            return MsgUtil.fail(MsgEnum.FAILURE.msg());
        } else {
            return MsgUtil.success(evaluationCommitteeMember);
        }
    }

    /**
     * 根据手机号和密码，获取成员信息
     * @param userDTO
     * @return
     */
    @PostMapping("/evaluationCommittee/findMembersByMobileAnPassword")
    public Message<EvaluationCommitteeMembersDTO> findMembersByMobileAnPassword(@RequestBody UserDTO userDTO) {
        if (null == userDTO || null == userDTO.getUsername() || null == userDTO.getPassword()) {
            return MsgUtil.fail("参数为空");
        }

        EvaluationCommitteeMembersDTO evaluationCommitteeMember = evaluationCommitteeMembersService.findByMemberMobileAndPassword(userDTO.getUsername(), userDTO.getPassword());

        if (evaluationCommitteeMember == null) {
            return MsgUtil.fail(MsgEnum.FAILURE.msg());
        } else {
            return MsgUtil.success(evaluationCommitteeMember);
        }
    }

    /**
     *
     * @Description: 专家登录之后获取评标信息
     * @Author: zhongdl
     * @Date:2022/2/15 9:59
     * @param evaluationCommitteeMembers:
     * @return: {@link Message< EvaluationCommitteeMembersDTO> }
     **/
    @PostMapping("/evaluationCommittee/getEvaluationInfo")
    public Message<EvaluationCommitteeMembersDTO> getEvaluationInfo(@RequestBody EvaluationCommitteeMembersDTO evaluationCommitteeMembers){
        //查询评委会信息
        EvaluationCommitteeMembers evaluationCommitteeMembersDB = evaluationCommitteeMembersService.getById(evaluationCommitteeMembers.getId());
        if (evaluationCommitteeMembersDB == null){
            return MsgUtil.fail(MsgEnum.NULL_RESULT.name());
        }
        EvaluationCommitteeMembersDTO evaluationCommitteeMembersDTO = evaluationCommitteeMembersDB.to(EvaluationCommitteeMembersDTO.class);

        // 是否投票结束
        boolean voteEndFlag = evaluationCommitteeMembersService.voteEndFlag(evaluationCommitteeMembersDB.getEvaluationCommitteeId());
        Integer voteEndFlagInt =  voteEndFlag ? LogicEnum.YES.getCode() : LogicEnum.NO.getCode();
        evaluationCommitteeMembersDTO.setVoteEndFlag(voteEndFlagInt);

        return MsgUtil.success(evaluationCommitteeMembersDTO);
    }

    /**
     * 保存专家的确认承诺书签字信息
     * @param confirmationInfoDTO
     * @return
     */
    @PostMapping("/evaluationCommittee/saveConfirmationInfo")
    public Message<Boolean> saveConfirmationInfo(@RequestBody ConfirmationInfoDTO confirmationInfoDTO) {
        //查询评委会信息
        EvaluationCommitteeMembers evaluationCommitteeMembersDB = evaluationCommitteeMembersService.getById(confirmationInfoDTO.getId());
        evaluationCommitteeMembersDB.setConfirmationFlag(LogicEnum.YES.getCode());
        evaluationCommitteeMembersDB.setConfirmationImage(confirmationInfoDTO.getConfirmationImage());
        boolean save = evaluationCommitteeMembersService.saveOrUpdate(evaluationCommitteeMembersDB);
        return MsgUtil.success(save);
    }


    /**
     *
     * @Description: 专家登录之后分页获取评标信息列表
     * @Author: zhongdl
     * @Date:2022/2/15 11:37
     * @param evaluationCommitteeBid:
     * @return: {@link Message<  PageResult <  EvaluationCommitteeBidDTO >> }
     **/
    @PostMapping("/evaluationCommittee/pageCommitteeBidBid")
    public   Message<PageResult<EvaluationCommitteeBidDTO>> pageCommitteeBidBid(@RequestBody EvaluationCommitteeBidDTO evaluationCommitteeBid){
        PageResult<EvaluationCommitteeBidDTO> pageResult = evaluationCommitteeBidService.pageCommitteeBidBid(evaluationCommitteeBid);
        if (CollUtil.isEmpty(pageResult.getRecords())){
            return MsgUtil.success(pageResult);
        }
        //判断是否是投票结束
        boolean voteEndFlag = evaluationCommitteeMembersService.voteEndFlag( evaluationCommitteeBid.getEvaluationCommitteeId());
        Integer voteEndFlagInt =  voteEndFlag ? LogicEnum.YES.getCode() : LogicEnum.NO.getCode();
        for (EvaluationCommitteeBidDTO record : pageResult.getRecords()) {
            // 如果评标状态是未开始，判断是否已经选举出了组长并且有任意一位评委签到，就算评标开始了
            if (record.getEvaluationStatus().equals(EvaluationStatusEnum.NO_STARTED.getCode())) {
                boolean started = evaluationCommitteeMembersService.evaluationStarted(evaluationCommitteeBid.getEvaluationCommitteeId());
                if (started) {
                    record.setEvaluationStatus(EvaluationStatusEnum.UNDERWAY.getCode());
                }
            }

            // 评标状态名称
            record.setEvaluationStatusName(Enums.of(EvaluationStatusEnum.class, record.getEvaluationStatus()).getShowName());

            // 是否投票结束
            record.setVoteEndFlag(voteEndFlagInt);

            // 填充招标文件信息
            if (record.getFileId() != null) {
                BidFileInfoDTO bidFileDetail = bidFileInfoService.getBidFileDetail(record.getFileId());
                if (bidFileDetail != null) {
                    record.setBidFileInfoDTO(bidFileDetail);

                    // 填充简易流程还是标准流程（1，简易，2，标准）
                    record.setSimpleOrStandard(bidFileDetail.getSimpleOrStandard());
                }
            }
        }

        return MsgUtil.success(pageResult);
    }

    /**
     *
     * @Description: 根据评委会id查询评委会成员
     * @Author: zhongdl
     * @Date:2022/2/15 13:50
     * @param evaluationCommitteeMembersDTO:
     * @return: {@link Message< List< EvaluationCommitteeBidDTO>> }
     **/
    @PostMapping("/evaluationCommittee/listEvaluationCommitteeMember")
    public Message<List<EvaluationCommitteeMembersDTO>> listEvaluationCommitteeMember(@RequestBody EvaluationCommitteeMembersDTO evaluationCommitteeMembersDTO){
        List<EvaluationCommitteeMembersDTO> committeeMembersDTOList = evaluationCommitteeMembersService.listEvaluationCommitteeMember(evaluationCommitteeMembersDTO.getEvaluationCommitteeId(),evaluationCommitteeMembersDTO.getMemberTypes());
        return MsgUtil.success(committeeMembersDTOList);
    }

    /**
     *
     * @Description: 更新评委会成员信息
     * @Author: zhongdl
     * @Date:2022/2/15 13:50
     * @param evaluationCommitteeMembersDTO:
     * @return: {@link Message< List< EvaluationCommitteeBidDTO>> }
     **/
    @PostMapping("/evaluationCommittee/updateCommitteeMembersInfo")
    public Message<Boolean> updateCommitteeMembersInfo(@RequestBody EvaluationCommitteeMembersDTO evaluationCommitteeMembersDTO){
        boolean update = evaluationCommitteeMembersService.updateById(evaluationCommitteeMembersDTO);
        return MsgUtil.success(update);
    }


    /**
     *
     * @Description: 获得是否投票结束
     * @Author: zhongdl
     * @Date:2022/2/15 14:38
     * @param evaluationCommitteeMembers:
     * @return: {@link Message< EvaluationCommitteeMembersDTO> }
     **/
    @PostMapping("/evaluationCommittee/getVoteEndFlag")
     public Message<EvaluationCommitteeMembersDTO> getVoteEndFlag(@RequestBody EvaluationCommitteeMembersDTO evaluationCommitteeMembers){

        //判断是否是投票结束
        boolean voteEndFlag = evaluationCommitteeMembersService.voteEndFlag( evaluationCommitteeMembers.getEvaluationCommitteeId());
        if (!voteEndFlag){
            evaluationCommitteeMembers.setVoteEndFlag(LogicEnum.NO.getCode());
            return MsgUtil.success(evaluationCommitteeMembers);

        }
        //已经结束投票 需要更新组长
        evaluationCommitteeMembers.setVoteEndFlag(LogicEnum.YES.getCode());
         // 根据票数获取组长的id
         Long leaderId = evaluationCommitteeMembersService.votesCountLeaderId(evaluationCommitteeMembers.getEvaluationCommitteeId());
         //更新组长
        EvaluationCommitteeMembers committeeMember = new EvaluationCommitteeMembers();
        committeeMember.setId(leaderId);
        committeeMember.setChairmanFlag(LogicEnum.YES.getCode());
        evaluationCommitteeMembersService.updateById(committeeMember);
        //设置组长id
        evaluationCommitteeMembers.setHeadManId(leaderId);
        return MsgUtil.success(evaluationCommitteeMembers);
    }


    /**
     *
     * @Description: 结束评标
     * @Author: zhongdl
     * @Date:2022/2/15 16:50
     * @param evaluationCommitteeBid:
     * @return: {@link Message< Boolean> }
     **/
    @PostMapping("/evaluationCommittee/endEvaluation")
    public Message<Boolean> endEvaluation(@RequestBody EvaluationCommitteeBidDTO evaluationCommitteeBid){
        //评委会结束标段的评标
        boolean endEvaluationFlag = evaluationCommitteeBidService.endEvaluation(evaluationCommitteeBid.getEvaluationCommitteeId(),evaluationCommitteeBid.getSectionId());
        if (!endEvaluationFlag){
            return MsgUtil.fail(MsgEnum.FAILURE.name());
        }
        //判断下评委会标段是否全部结束评标
        boolean endAllSectionEvaluationFlag = evaluationCommitteeBidService.endAllSectionEvaluationFlag(evaluationCommitteeBid.getEvaluationCommitteeId());
        if (!endAllSectionEvaluationFlag){
            return MsgUtil.success(endEvaluationFlag);

        }
        //如果已经结束评标  就删除所有的评委会成员
        boolean removeFlag = evaluationCommitteeMembersService.removeMembersByEvaluationCommitteeId(evaluationCommitteeBid.getEvaluationCommitteeId());
        return MsgUtil.success(removeFlag);

    }
    /**
    * Description: 根据标段id查询当前标段下的专家身份证号
    * @author: fengbin
    * @date: 2022/2/24 15:36
    * @param sectionId
     @return: org.fzzn.core.model.msg.Message<java.util.List<java.lang.String>>
    */
    @GetMapping("/listExpertIdCardBySectionId/listExpertIdCardBySectionId")
    public Message<List<String>> listExpertIdCardBySectionId(@RequestParam("sectionId") Long sectionId) {
        List<String> list = evaluationCommitteeMembersService.listExpertIdCardBySectionId(sectionId);
        return MsgUtil.success(list);
    }


    /**
     * 重新指定专家
     *
     * @param evaluationCommitteeDTO
     * @return org.fzzn.core.model.msg.Message<java.lang.String>
     * @author tian
     * @date 2022-4-18 9:11
     */
    @PostMapping("/evaluationCommittee/reassignExpert")
    public Message<String> reassignExpert(@RequestBody EvaluationCommitteeDTO evaluationCommitteeDTO) {
        return evaluationCommitteeMembersService.reassignExpert(evaluationCommitteeDTO);
    }

    /**
     * 重新指定组长
     *
     * @param evaluationCommitteeDTO
     * @return org.fzzn.core.model.msg.Message<java.lang.String>
     * @author tian
     * @date 2022-4-18 9:11
     */
    @PostMapping("/evaluationCommittee/reassignLeaderExpert")
    public Message<String> reassignLeaderExpert(@RequestBody EvaluationCommitteeDTO evaluationCommitteeDTO) {
        return evaluationCommitteeMembersService.reassignLeaderExpert(evaluationCommitteeDTO);
    }

    /**
     * @param sectionId:根据标段id查询专家列表
     * @Description:
     * @Author: zhongdl
     * @Date:2022/4/22 9:35
     * @return: {@link Message< List< EvaluationCommitteeMembers>> }
     **/
    @PostMapping("/evaluationCommittee/listMembersBySectionId")
    public Message<List<EvaluationCommitteeMembers>> listMembersBySectionId(@RequestParam Long sectionId) {
        List<EvaluationCommitteeMembers> list = evaluationCommitteeMembersService.findBySectionId(sectionId);
        return MsgUtil.success(list);
    }




}
